#pragma once

#include "parentpanel.h"
#include "sclippalletteentry.h"
#include "templatevectorlist.h"
#include "sprite.h"

class SClipPallette : public ParentPanel
{
private:
	TemplateVectorList<SClipPalletteEntry> m_clipEntries;
	TemplateVectorList<ImageClip> m_imgClips;
	SClipPalletteEntry * m_selected;

	static void selectClip(void * a_clipEntry)
	{
		SClipPalletteEntry * selected = (SClipPalletteEntry*)a_clipEntry;
		SClipPallette * scp = (SClipPallette *)selected->getParent();
		scp->setSelected(selected);
		scp->setDirty(0);
	}
public:
	void release()
	{
		m_clipEntries.release();
		m_imgClips.release();
	}
	~SClipPallette()
	{
		release();
	}
	void draw(SDL_Surface * a_surface)
	{
		ParentPanel::draw(a_surface);
		if(m_selected)
		{
			SRect * buttonArea = m_selected->getArea();
			VSDL_drawRect(a_surface, buttonArea, -1, 2);
		}
	}
	void setSelected(SClipPalletteEntry * a_entry)
	{
		m_selected = a_entry;
	}
	SClipPallette():ParentPanel(),m_selected(0),m_clipEntries(10),m_imgClips(10)
	{setChildrenScrollable(true);setAutoArrangeGrid(true);setDragAndDropChild(true);}

	TemplateVectorList<SClipPalletteEntry> * getClipEntries(){return &m_clipEntries;}

	int getClipCount(){return m_clipEntries.size();}

	ImageClip * getClip(const int & a_index){return m_clipEntries.get(a_index).getClipEntry();}

	ImageClip * getSelectedClip()
	{
		if(m_selected)
			return m_selected->getClipEntry();
		return 0;
	}
	void setArea(const SRect & a_rect)
	{
		ParentPanel::setArea(a_rect);
		requestChildAreaRefresh();
		refreshArea();
	}

	bool isThisClipAlreadyHere(const ImageClip & a_clip)
	{
		bool hasThisClip = false;
		for(int i = 0; !hasThisClip && i < m_clipEntries.size(); ++i)
		{
			hasThisClip = m_clipEntries.get(i).getClipEntry()->equals(a_clip);
		}
		return hasThisClip;
	}

	/** adds a clip that is from an existing location (allocated by sprite) */
	void addClip(ImageClip * a_clip)
	{
		if(!isThisClipAlreadyHere(*a_clip))
		{
			m_clipEntries.add(SClipPalletteEntry(a_clip));
			SClipPalletteEntry * lastEntry = &m_clipEntries.getLast();
			lastEntry->setParent(this);
			lastEntry->refreshArea();
			lastEntry->setAction((ButtonCallbackFunction)selectClip, lastEntry);
			addChild(lastEntry);
			requestChildAreaRefresh();
		}
	}
	/** adds a temporary clip that will be copied in a more permanent location */
	void addClip(const ImageClip & a_clip)
	{
		if(!isThisClipAlreadyHere(a_clip))
		{
			m_imgClips.add(a_clip);
			ImageClip * lastClip = &m_imgClips.getLast();
			addClip(lastClip);
			//m_clipEntries.add(SClipPalletteEntry(lastClip));
			//SClipPalletteEntry * lastEntry = &m_clipEntries.getLast();
			//lastEntry->setParent(this);
			//addChild(lastEntry);
			//requestChildAreaRefresh();
		}
	}

	void addSpriteClips(Sprite * s)
	{
		SAnimation * sa;
		SFrame * sf;
		SClip * sc;
		for(int a = 0; a < s->getAnimationCount(); ++a)
		{
			sa = s->getAnimation(a);
			for(int f = 0; f < sa->getFrameCount(); ++f)
			{
				sf = sa->getFrame(f);
				for(int c = 0; c < sf->getClipCount(); ++c)
				{
					sc = sf->getClip(c);
					addClip(sc->getClip());
				}
			}
		}
	}
};